# -*- coding: gb2312 -*-
import os
import base64
import paramiko
import threading
from Crypto.Cipher import AES
from ftplib import FTP

file_sftp = 'sshlist'
file_ftp = 'telnetlist'
dir_log = 'logs'
list_thread = []
mv_file = False    
createDir = lambda dirname: not os.path.exists(dirname) and os.mkdir(dirname)

class SftpDownloader(threading.Thread):
    def __init__(self, items):
        self.db_name = items[2].strip()
        self.ip_addr = items[1].strip()
        self.host_pswd = items[6].strip()
        self.remote_alert_pathname = items[4].strip() + '/' + items[5].strip()
        self.local_alert_pathname= dir_log + '/' + items[5].strip()
        super(SftpDownloader, self).__init__(name = self.db_name)  #set thread name
    def run(self):   
        print '[INFO]Start to run SftpDownloader, thread  -->', self.db_name, ', downloading alert_log -->', self.remote_alert_pathname
        self.down_log()
    def down_log(self):
        socks=(self.ip_addr, 22)
        ssh=paramiko.Transport(socks)
        ssh.connect(username = 'oracle', password=self.host_pswd)
        sftptest=paramiko.SFTPClient.from_transport(ssh)
        sftptest.get(self.remote_alert_pathname, self.local_alert_pathname)
        sftptest.close()
        ssh.close()

class FtpDownloader(threading.Thread):
    def __init__(self, items):
        self.db_name = items[2].strip()
        self.ip_addr = items[1].strip()
        self.host_pswd = items[6].strip()
        self.remote_alert_path = items[4].strip()
        self.remote_alert_file = items[5].strip()
        self.local_alert_pathname= dir_log + '/' + items[5].strip()
        super(FtpDownloader, self).__init__(name = self.db_name)                   #set thread name
    def run(self):
        print '[INFO]Start to run FtpDownloader, thread  -->', self.db_name, ', downloading alert_log -->', self.remote_alert_file
        self.down_log()
    def down_log(self):
        ftp = FTP()
        ftp.connect(self.ip_addr, 21)
        ftp.login('oracle', self.host_pswd)
        #print ftp.getwelcome()
        ftp.cwd(self.remote_alert_path)
        fd_wt = open(self.local_alert_pathname, 'wb')
        buf_size=4096
        ftp.retrbinary('RETR ' + self.remote_alert_file, fd_wt.write, buf_size)
        fd_wt.close();
        ftp.quit()

def decode_line(cipher_text):
    secret = '16u8HrtxZRU7dXoOGx9bbNv0IynF1K4N'
    decode_AES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip('{')
    cipher_machine =  AES.new(secret)                               
    line = decode_AES(cipher_machine, cipher_text)                          #decode the cipher text
    return line.split('\t')

if  __name__  ==  '__main__':
    createDir(dir_log)
    fd_rd = open(file_sftp, mode = 'r', buffering = -1)
    for line in fd_rd:
        list_thread.append(SftpDownloader(decode_line(line)))
    fd_rd.close()
    
    fd_rd = open(file_ftp, mode = 'r', buffering = -1)
    for line in fd_rd:
        list_thread.append(FtpDownloader(decode_line(line)))
    fd_rd.close()

    #getcwd = lambda curwd: curwd == '/' and '/' or (curwd + '/') 
    #start threads and join the main thread
    for t in list_thread:
        t.start()
    for t in list_thread:
        t.join()
    
    print '----Completed!-----'